package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmConsignee;
import com.nuoniu.sibanyun.service.crm.ICrmConsigneeService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 收货人信息 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/crm/consignee")
@Api(value="收货人信息 ",tags={"收货人信息"})
public class CrmConsigneeController extends BaseController {

    @Autowired
    private ICrmConsigneeService consigneeService;

    /**
     * 获取收货人列表
     * @author 小啊刚
     * @date 2020/6/24
     */
    @GetMapping("page")
    @ApiOperation(value = "分页",tags = "APP_收货人信息")
    public HttpResult<Page<CrmConsignee>> getConsigneeList(Page page, CrmConsignee crmConsignee) {
        Page<CrmConsignee> crmConsigneePage = consigneeService.getConsigneeList(page, crmConsignee);
        return HttpResult.ok(crmConsigneePage);
    }

    /**
     * 保存收货人
     * @author 小啊刚
     * @date 2020/6/24
     */
    @PostMapping("save")
    @ApiOperation(value = "保存",tags = "APP_收货人信息")
    public HttpResult saveConsignee(@RequestBody CrmConsignee crmConsignee) {
        UserInfo userInfo = UserThreadLocal.get();
        if(crmConsignee.getId() == null) {
            crmConsignee.setCreateUserId(userInfo.getUserId());
            crmConsignee.setCreateUserName(userInfo.getRealName());
        }else {
            crmConsignee.setUpdateUserId(userInfo.getUserId());
            crmConsignee.setUpdateDate(new Date());
            crmConsignee.setUpdateUserName(userInfo.getRealName());
        }
        consigneeService.saveOrUpdate(crmConsignee);
        return HttpResult.ok();
    }

//    /**
//     * 删除收货人
//     * @author 小啊刚
//     * @date 2020/6/24
//     */
//    @GetMapping("delete")
//    @ApiOperation(value = "删除",tags = "APP_收货人信息")
//    public HttpResult deleteConsignee(String ids) {
//        if(StringUtils.isNotBlank(ids)) {
//            for(String id : ids.split(",")) {
//                consigneeService.removeById(Long.valueOf(id));
//            }
//        }
//        return HttpResult.ok();
//    }

    /**
     * 停用收货人
     */
    @GetMapping(value = "disable")
    @ApiOperation(value = "停用",tags = "APP_收货人信息")
    public HttpResult disableSelected(String ids) {
        if (StringUtils.isNoneBlank(ids)) {
            for (String id : ids.split(",")) {
                CrmConsignee crmConsignee = new CrmConsignee();
                crmConsignee.setId(Long.valueOf(id));
                crmConsignee.setStatus(2);
                consigneeService.updateById(crmConsignee);
            }
        }
        return HttpResult.ok("停用收货人成功");
    }

    /**
     * 启用收货人
     */
    @GetMapping(value = "enable")
    @ApiOperation(value = "启用",tags = "APP_收货人信息")
    public HttpResult enableSelected(String ids) {
        if (StringUtils.isNoneBlank(ids)) {
            for (String id : ids.split(",")) {
                CrmConsignee crmConsignee = new CrmConsignee();
                crmConsignee.setId(Long.valueOf(id));
                crmConsignee.setStatus(0);
                consigneeService.updateById(crmConsignee);
            }
        }
        return HttpResult.ok("启用收货人成功");
    }
}
